﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class admin_ednews : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        String id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";

        String lang = Request.QueryString["lang"] != null ? Request.QueryString["lang"].ToString() : "";
        String sql = "select * from [News] where id = @id and lang = @lang";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("id", id);
        cmd.Parameters.AddWithValue("lang", lang);
        //try
        //{
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.HasRows)
        {
            rd.Read();
            hdfNewID.Value = rd.GetInt64(0).ToString();
            //Response.Write("<script language='javascript'>alert('" + rd.GetSqlString(1) + "');</script>");
            ddlLang.SelectedValue = rd.GetSqlString(1).ToString();
            //ddlLang.SelectedValue = rd.GetSqlString(1) != null ? rd.GetSqlString(1).ToString(): "";
            title.Text = rd.GetSqlString(2).ToString();
            des.Text = rd.GetSqlString(3).ToString();
            fckContent.Value = rd.GetSqlString(4).ToString();
            newstype.SelectedValue = rd.GetInt64(5).ToString();
            hdfImage.Value = rd.GetSqlString(6).ToString();
        }
        rd.Close();
        cn.Close();
        //}
        //catch (Exception ex)
        //{
        //    Response.Redirect("Default.aspx?page=qljobdetail");
        //}
    }
    protected void addbt_Click(object sender, EventArgs e)
    {
        String regex = "";
        if (fulImage.PostedFile.ContentLength > 2097151)
        {
            lbImage.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
            return;
        }
        else
        {
            regex = System.IO.Path.GetExtension(fulImage.PostedFile.FileName.ToLower().ToString());
            String path = "~/image/";
            DeleteImage(path + hdfImage.Value);
            if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
            {
                fulImage.SaveAs(Server.MapPath(path+hdfNewID.Value+ regex));
            }
            else if (regex != "")
            {
                lbImage.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
            }
        }
        if (regex == "")
        {
            //url.Value = null;
        }
        else
        {
            //url.Value = img.PostedFile.FileName;
        }
        hdfImage.Value = hdfNewID.Value + regex;
        hdfContent.Value = fckContent.Value;
        SqlDataSource2.Update();
        SqlDataSource2.DataBind();
        Response.Write("<script>alert('Update Thành Công')</script>");
        Response.Redirect("Default.aspx?page=qlNews");
    }
    private void DeleteImage(String imageName)
    {
        String path = this.Server.MapPath(imageName);
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        catch (Exception ex)
        {
            //Response.Write("<script language='javascript'>alert('" + ex + "');</script>");
        }
    }
}